package com.miyake.dsp.osa.lab.wrapper;

import com.miyake.dsp.osa.OsaSetting;
import com.miyake.dsp.osa.OsaSetting.VBW;
import com.miyake.dsp.osa.lab.Calculator;
import com.miyake.dsp.osa.lab.LabUiAnnotation;
import com.miyake.realinstrument.MS9740A;

@LabUiAnnotation(caption="MS9740A")
public class Ms9740aWrapper extends Calculator {
	public static final String OUT_LEVEL = "Level";
	public static final String OUT_WAVELENGTH = "Wavelength";
	private OsaSetting setting;
	private MS9740A ms9740a;
	
	public Ms9740aWrapper(OsaSetting setting) {
		ms9740a = new MS9740A();
		ms9740a.runWebServer();
		ms9740a.connect("192.168.0.10");
		this.setting = setting;
		initOutput(OUT_LEVEL);
		initOutput(OUT_WAVELENGTH);
	};
	
	@Override
	public void process() {
		
		ms9740a.write("CNT " + setting.getCenterWavelength());
		ms9740a.write("SPN " + setting.getSpanWavelength());
		ms9740a.write("MPT " + setting.getSamplingPoints());
		ms9740a.write("VBW " + getVBW());
		ms9740a.single();
		setOutput(OUT_LEVEL, ms9740a.getTrace());
		setOutput(OUT_WAVELENGTH, getWavelength());
		
	}

	private double[] getWavelength() {
		double[] ret = new double[setting.getSamplingPoints()];
		double step = (setting.getSpanWavelength() / (setting.getSamplingPoints()-1));
		double start = setting.getCenterWavelength() - setting.getSpanWavelength()/2.0;
		for (int i = 0; i < ret.length; i++) {
			ret[i] = start + i * step;
		}
		return ret;
	}

	private String getVBW() {
		if (setting.getVbw().equals(VBW._1MHz)) {
			return "1MHz";
		}
		else if (setting.getVbw().equals(VBW._100kHz)) {
			return "100kHz";
		}
		else if (setting.getVbw().equals(VBW._10kHz)) {
			return "10kHz";
		}
		else if (setting.getVbw().equals(VBW._2kHz)) {
			return "2kHz";
		}
		else if (setting.getVbw().equals(VBW._1kHz)) {
			return "1kHz";
		}
		else if (setting.getVbw().equals(VBW._200Hz)) {
			return "200Hz";
		}
		else if (setting.getVbw().equals(VBW._100Hz)) {
			return "100Hz";
		}
		return "10Hz";
	}

	@Override
	public Object getSetting() {
		return this.setting;
	}

}
